﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class OutboundOrderProductSearchHander : IRequestHandler<OutboundOrderProductSearchCommand, ResultT<OutboundOrderProductsDto>>
    {

        private readonly IBaseRepositorys<OutboundOrderProduct> outboundOrderProductRepository;
        private readonly IBaseRepositorys<Product> productRepository;
        public OutboundOrderProductSearchHander(IBaseRepositorys<OutboundOrderProduct> outboundOrderProductRepository, IBaseRepositorys<Product> productRepository)
        {
            this.outboundOrderProductRepository = outboundOrderProductRepository;
            this.productRepository = productRepository;
        }


        /// <summary>
        /// 根据选的出库单查询出库单商品
        /// </summary>
        /// <param name="request">出库单商品查询请求对象</param>
        /// <param name="cancellationToken">允许取消的Token</param>
        /// <returns>返回出库单产品列表</returns>
        public Task<ResultT<OutboundOrderProductsDto>> Handle(OutboundOrderProductSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultT<OutboundOrderProductsDto>();
            var list = (from a in outboundOrderProductRepository.GetAll().Where(c=>c.OutboundOrderId==request.OutboundOrderId)
                        join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                        select new OutboundOrderProductsDto
                        {
                            ProductId = a.ProductId,
                            ProductNumber = b.ProductNumber,
                            ProductName = b.ProductName,
                            ProductSpecification = b.ProductSpecification,
                            StockUnit = b.StockUnit == 1 ? "条" : b.StockUnit == 2 ? "张" : b.StockUnit == 3 ? "车" : b.StockUnit == 4 ? "车" : b.StockUnit == 5 ? "天" : b.StockUnit == 6 ? "小时" : b.StockUnit == 7 ? "分钟" : b.StockUnit == 8 ? "周" : b.StockUnit == 9 ? "月" : "",
                            MaxInventory = b.MaxInventory,
                            MinInventory = b.MinInventory,
                            SaveInventory = b.SaveInventory,
                            InventoryStore = b.InventoryStore,
                            OutNum = a.OutNum,
                        }).ToList();
            result.Data = list;
            return Task.FromResult(result);

        }
    }
}
